//-----------------Sonic CD Torch Script-------------------//
// Yeah...

// Aliases
#alias Object.Value1	:	Object.Timer
#alias Object.Value2	:	Object.YDrawPos
#alias Object.Value3	:	Object.T1
#alias Object.Value4	:	Object.T2
#alias Object.Value5	:	Object.T3
#alias Object.Value5	:	Object.T4
#alias Object[24].PropertyValue	:	HUD.CurrentTimePeriod


#alias Object.PropertyValue		:	PushButton.Pressed	

 
// Priority
#alias 0	:	PRIORITY_BOUNDS
#alias 1	:	PRIORITY_ACTIVE

// States
#alias 0	:	FIRE_OFF
#alias 1	:	FIRE_ON

sub ObjectMain
	if PushButton[-1].Pressed == false
			Object.T4 = 0
	end if
	if Object.T4 == 0
		if PushButton[-1].Pressed == true
			if	Object.State == FIRE_OFF
				Object.State    = FIRE_ON
				PlayStageSFX(6, false)
			else
				Object.State    = FIRE_OFF
			end if
			Object.T4 = 1
		end if
	end if
end sub

sub ObjectDraw
	DrawSprite(0)
	TempValue0 = 0
	TempValue1 = 16
	while TempValue0 < Object.PropertyValue
		TempValue3  = -512
		TempValue3  *= TempValue1
		TempValue3 <<= 7
		TempValue3  += Object.YPos

		DrawSpriteXY(1, Object.XPos, TempValue3)

		TempValue0++

		TempValue1 += 16
	loop
	if Object.State == FIRE_ON
		Object.Timer--
		if Object.Timer < 0
			Object.Timer = 7
			if Object.Frame > 8
				Object.Frame = 3
			else
				Object.Frame++
			end if
		end if
		DrawSpriteXY(Object.Frame, Object.XPos, Object.YDrawPos)
	else
	end if

	Object.YDrawPos = -512
	Object.YDrawPos  *= TempValue1
	Object.YDrawPos <<= 7
	Object.YDrawPos  += Object.YPos
	Object.YDrawPos  &= 0xFFFF0000 // Truncate the value

	DrawSpriteXY(2, Object.XPos, Object.YDrawPos)
end sub


sub ObjectStartup
	LoadSpriteSheet("R2/Objects3.gif")

	TempValue0 = HUD.CurrentTimePeriod
	TempValue1 = 69
	TempValue0 *= 27
	TempValue1 += TempValue0 
	
	SpriteFrame(-13, -8, 26, 16, TempValue1, 35)	// #0 - Bottom
	SpriteFrame(-13, -8, 26, 16, TempValue1, 18)		// #1 - Tube
	SpriteFrame(-13, -8, 26, 16, TempValue1, 1)		// #2 - Top
	
	SpriteFrame(-15, -40, 30, 32, 177, 1)		// #3 - Fire Frame 1
	SpriteFrame(-15, -40, 30, 32, 208, 1)		// #4 - Fire Frame 2
	SpriteFrame(-15, -40, 30, 32, 177, 34)		// #5 - Fire Frame 3
	SpriteFrame(-15, -40, 30, 32, 208, 34)		// #6 - Fire Frame 4
	SpriteFrame(-15, -40, 30, 32, 177, 67)		// #7 - Fire Frame 5
	SpriteFrame(-15, -40, 30, 32, 208, 67)		// #8 - Fire Frame 6
	SpriteFrame(-15, -40, 30, 32, 177, 100)		// #9 - Fire Frame 7
	
	
	// Set this object to always active
	ArrayPos0 = 32
	while ArrayPos0 < 1056
		if Object[ArrayPos0].Type == TypeName[Torch]
			Object[ArrayPos0].Priority = PRIORITY_ACTIVE
		end if
		ArrayPos0++
	loop
end sub

// ========================
// Editor Subs
// ========================

sub RSDKDraw
	DrawSprite(0)
	TempValue0 = 0
	TempValue1 = 16
	while TempValue0 < Object.PropertyValue
		TempValue3  = -512
		TempValue3  *= TempValue1
		TempValue3 <<= 7
		TempValue3  += Object.YPos

		DrawSpriteXY(1, Object.XPos, TempValue3)

		TempValue0++

		TempValue1 += 16
	loop
	
	Object.YDrawPos = -512
	Object.YDrawPos  *= TempValue1
	Object.YDrawPos <<= 7
	Object.YDrawPos  += Object.YPos
	Object.YDrawPos  &= 0xFFFF0000 // Truncate the value
	
	DrawSpriteXY(2, Object.XPos, Object.YDrawPos)
	if editor.showGizmos == true
		editor.drawingOverlay = true

		//Draw an arrow to the button associated with this torch object
		DrawArrow(object.xpos, object.ypos, object[-1].xpos, object[-1].ypos, 255, 255, 255)

		//Draw the fire for a little flair
		object.inkEffect = 1
		DrawSpriteFX(3, FX_INK, object.xpos, Object.YDrawPos)
		object.inkEffect = 0

		editor.drawingOverlay = false
	end if
end sub


sub RSDKLoad
	LoadSpriteSheet("R2/Objects3.gif")
	SpriteFrame(-13, -8, 26, 16, 69, 35)	// #0 - Bottom
	SpriteFrame(-13, -8, 26, 16, 69, 18)		// #1 - Tube
	SpriteFrame(-13, -8, 26, 16, 69, 1)		// #2 - Top

	//The fire will only be drawn when editor.showGizmos is on
	SpriteFrame(-15, -40, 30, 32, 177, 1)		// #3 - Fire Frame 1

	SetVariableAlias(ALIAS_VAR_PROPVAL, "height")
end sub